package com.fan.reggie.utils;

//import jakarta.mail.internet.MimeMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

@Service
public class emailUtils {

    @Autowired(required = false)
    private JavaMailSender javaMailSender;

    @Value("${spring.mail.username}")
    private String mailFrom;

    public void sendVerificationCode(String toEmail, String code) {
        try {
            MimeMessage message = javaMailSender.createMimeMessage();

            MimeMessageHelper helper = new MimeMessageHelper(message, true);

            helper.setFrom(mailFrom); // 设置发件人邮箱
            helper.setTo(toEmail); // 设置收件人邮箱
            helper.setSubject("Verification Code"); // 设置邮件主题
            helper.setText("Your verification code is: " + code); // 设置邮件内容

            javaMailSender.send(message);

        } catch (MessagingException e) {
            e.printStackTrace();
            // 处理邮件发送异常
        }
    }
}
